package LeetCode;

public class LC38 {
    public String countAndSay(int n) {
        if(n==1) return "1";
        return change(countAndSay(n-1));
    }
    public static String change(String str){
        StringBuilder sb = new StringBuilder();
        char a;
        int number;
        for (int i = 0; i < str.length(); i++) {
            a = str.charAt(i);
            number=1;
            i++;
            if(i==str.length()) break;
            while (str.charAt(i)==a){
                number++;
                i++;
                if(i==str.length()) break;
            }
            sb.append(number);
            sb.append(a);
        }
        return sb.toString();
    }
}